package org.jeecg.modules.online.cgreport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportHead;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportItemService;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Online报表API Service
 */
@Slf4j
@Service("onlCgreportAPIService")
public class OnlCgreportAPIService implements IOnlCgreportAPIService {

    @Autowired
    private OnlCgreportHeadServiceImpl onlCgreportHeadService;
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;

    @Override
    public Map<String, Object> getDataById(String id, Map<String, Object> params) {
        return this.getData(id, null, params);
    }

    @Override
    public Map<String, Object> getDataByCode(String code, Map<String, Object> params) {
        return this.getData(null, code, params);
    }

    @Override
    public Map<String, Object> getData(String id, String code, Map<String, Object> params) {
        OnlCgreportHead head = null;
        if (StringUtil.isNotEmpty(id)) {
            head = onlCgreportHeadService.getById(id);
        } else if (StringUtil.isNotEmpty(code)) {
            LambdaQueryWrapper<OnlCgreportHead> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OnlCgreportHead::getCode, code);
            head = onlCgreportHeadService.getOne(queryWrapper);
        }
        if (head == null) {
            throw new JeecgBootException("实体不存在");
        }
        try {
            String cgrSql = head.getCgrSql().trim();
            String dbKey = head.getDbSource();
            //加载数据权限配置 和 系统内置变量
            //String sql = SqlUtil.rewriteReportSql(cgrSql);
            return this.executeSelectSqlRoute(dbKey, cgrSql, params, head.getId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new JeecgBootException("SQL执行失败：" + e.getMessage());
        }
    }

    @Override
    public Map<String, Object> executeSelectSqlRoute(String dbKey, String sql, Map<String, Object> params, String headId) throws Exception {
        if (StringUtils.isNotBlank(dbKey)) {
            //log.debug("Online报表: 走了多数据源逻辑");
            return onlCgreportHeadService.executeSelectSqlDynamic(dbKey, sql, params, headId);
        } else {
            //log.debug("Online报表: 走了稳定逻辑");
            return onlCgreportHeadService.executeSelectSql(sql, headId, params);
        }
    }

}
